var Class = {
    create: function () {
        return function () {
            this.initialize.apply(this, arguments)
        }
    }
};
Object.extend = function (a, c) {
    for (var b in c) {
        a[b] = c[b]
    }
    return a
};
var Calendar = Class.create();
Calendar.prototype = {
    initialize: function (a, b) {
        this.Container = document.getElementById(a);
        this.Days = [];
        this.SetOptions(b);
        this.Year = this.options.Year;
        this.Month = this.options.Month;
        this.SelectDay = this.options.SelectDay ? new Date(this.options.SelectDay) : null;
        this.onSelectDay = this.options.onSelectDay;
        this.onToday = this.options.onToday;
        this.onFinish = this.options.onFinish;
        this.onPassedDay = this.options.onPassedDay;
        this.Draw()
    },
    SetOptions: function (a) {
        this.options = {
            Year: new Date().getFullYear(),
            Month: new Date().getMonth() + 1,
            SelectDay: null,
            onSelectDay: function () { },
            onToday: function () { },
            onFinish: function () { }
        };
        Object.extend(this.options, a || {})
    },
    GetDatePrefix: function () {
        if ((this.Month.length) < 2 && parseInt(this.Month) < 10) {
            this.Month = "0" + this.Month
        }
        return this.Year + "-" + this.Month + "-"
    },
    ShowMonth: function (b, a) {
        var c = new Date(b, a, 1);
        this.Year = c.getFullYear();
        this.Month = c.getMonth() + 1;
        this.Draw()
    },
    PreMonth: function () {
        var a = new Date(this.Year, this.Month - 2, 1);
        this.Year = a.getFullYear();
        this.Month = a.getMonth() + 1;
        this.Draw()
    },
    NextMonth: function () {
        var a = new Date(this.Year, this.Month, 1);
        this.Year = a.getFullYear();
        this.Month = a.getMonth() + 1;
        this.Draw()
    },
    Draw: function () {
        var b = [];
        for (var e = 1,
        c = new Date(this.Year, this.Month - 1, 1).getDay(); e <= c; e++) {
            b.push("&nbsp;")
        }
        for (var e = 1,
        f = new Date(this.Year, this.Month, 0).getDate(); e <= f; e++) {
            b.push(e)
        }
        var j = document.createDocumentFragment();
        this.Days = [];
        while (b.length > 0) {
            var h = document.createElement("tr");
            for (var e = 1; e <= 7; e++) {
                var a = document.createElement("td");
                a.innerHTML = "&nbsp;";
                if (b.length > 0) {
                    var g = b.shift();
                    a.innerHTML = g;
                    if (g > 0) {
                        if (e == 1 || e == 7) {
                            a.className = "day restDay"
                        } else {
                            a.className = "day"
                        }
                        this.Days[g] = a;
                        if (this.IsSame(new Date(this.Year, this.Month - 1, g), new Date())) {
                            this.onToday(a)
                        }
                        if (this.IsSmall(new Date(this.Year, this.Month - 1, g), new Date()) && !this.IsSame(new Date(this.Year, this.Month - 1, g), new Date())) {
                            this.onPassedDay(a)
                        }
                        if (this.SelectDay && this.IsSame(new Date(this.Year, this.Month - 1, g), this.SelectDay)) {
                            this.onSelectDay(a)
                        }
                    }
                }
                h.appendChild(a)
            }
            j.appendChild(h)
        }
        while (this.Container.hasChildNodes()) {
            this.Container.removeChild(this.Container.firstChild)
        }
        this.Container.appendChild(j);
        this.onFinish()
    },
    IsSame: function (b, a) {
        return (b.getFullYear() == a.getFullYear() && b.getMonth() == a.getMonth() && b.getDate() == a.getDate())
    },
    IsSmall: function (b, a) {
        return (b < a)
    }
};